package cn.zy.framework.helper;

import cn.zy.framework.util.ReflectionUtil;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @Author: Administrator
 * @Description:
 * @Date: Created in 17:24 2021/9/10
 * @Modified By:
 * @version: 1.0
 * Bean 助手类
 */
public final class BeanHelper {

	/**
	 * 定义Bean 映射（用于存放 Bean 类与 Bean 实例的映射关系）
	 */
	private static final Map<Class<?>, Object> BEAN_MAP = new HashMap<Class<?>, Object>();

	static {
		Set<Class<?>> beanClassSet = ClassHelper.getBeanClassSet();
		for (Class<?> beanClass : beanClassSet){
			Object obj = ReflectionUtil.newInstance(beanClass);
			BEAN_MAP.put(beanClass, obj);
		}
	}

	/**
	 * 获取 Bean 映射
	 * @return
	 */
	public static Map<Class<?>, Object> getBeanMap(){
		return BEAN_MAP;
	}

	public static <T> T getBean(Class<T> cls) {

		if (BEAN_MAP.containsKey(cls)){
			return (T) BEAN_MAP.get(cls);
		}
		throw new RuntimeException(" can not get bean by class: " + cls);

	}




}
